package com.ruoyi.project.monitor.job.task;

import com.ruoyi.project.monitor.job.domain.Point3D;
import com.ruoyi.project.system.coordinates.domain.Point3DTools;
import com.ruoyi.project.system.coordinates.service.ITbCoordinatesService;
import com.ruoyi.project.system.setting.domain.TbParamSetting;
import com.ruoyi.project.system.setting.service.ITbParamSettingService;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author kangjie
 * @className
 * @Description 获取点源定时任务
 * @Date 2019/12/17    \
 * @Version 1.0
 */
@Component("getPointSource")
public class GetPointSourceJob {
	@Autowired
	ITbCoordinatesService iTbCoordinatesService;
	
	@Autowired
	ITbParamSettingService iTbParamSettingService;


    public interface DllService extends Library {
    	
        /**
         * 读取云台接口数据
         */
        DllService INSTANCE = (DllService) Native.loadLibrary("LMS_Interface", DllService.class);

        boolean LSM_IsThreadRunningOK(int deviceIndex);

        /**
                * 设置参数
         * @param deviceIndex 设备号
         * @param startAngle 起始角度
         * @param stopAngle 终止角度
         * @param vel 扫描速度
         * @return
         */
        int LSM_SetScanParameter(int deviceIndex, int startAngle, int stopAngle, int vel);
        
        /**
                * 开启单次扫描
         * @param deviceIndex
         * @return
         */
        int  LSM_Start3DScanOnce(int deviceIndex);
        /**
                * 获取点云数据
         * @param deviceIndex 设备号
         * @param arr 点云结构体
         * @param len 长度
         * @param rows 行数
         * @return
         */
        int LSM_Get3DPointCloud(int deviceIndex, Point3D[] arr, int[] len , IntByReference rows);

        /**
         * 获取new 点云
         * @param deviceIndex
         * @param arr
         * @param len
         * @param rows
         * @return
         */
        int LSM_GetNewest3DPointCloud(int deviceIndex, Point3D[] arr, int[] len , IntByReference rows);

    }
    public GetPointSourceJob(){

    }

    /**
         * 获取点源执行函数
     */
    public Point3D[] getPointSource() throws InterruptedException {
        TbParamSetting paramSetting = iTbParamSettingService.getParam();
        int StartAngle = paramSetting.getStartAngle().intValue();
        int stopAngle = paramSetting.getEndAngle().intValue();
        int vel = paramSetting.getSpeed().intValue();
        //运行是否ok
        System.out.println("test dll LSM_IsThreadRunningOK start ");
        System.out.println("LSM_IsThreadRunningOK() :"+DllService.INSTANCE.LSM_IsThreadRunningOK(1));
        System.out.println("test dll LSM_IsThreadRunningOK end ");
        //设置参数
        System.out.println("test dll  set param start");
        System.out.println("LMS set param ():" + DllService.INSTANCE.LSM_SetScanParameter(1,StartAngle,stopAngle,vel));
        System.out.println("test dll  set param :StartAngle="+StartAngle+";stopAngle="+stopAngle+";vel="+vel+" end");
        //开始单次扫描
        System.out.println("test dll  LSM_Start3DScanOnce start");
        System.out.println("test dll start LSM_Start3DScanOnce():"+DllService.INSTANCE.LSM_Start3DScanOnce(1));
        System.out.println("test dll  LSM_Start3DScanOnce end");
        Thread.sleep(20000);
        int nCount = 500000;
        Point3D point3DReferenceArr=new Point3D();
        Point3D[] arr = (Point3D[])point3DReferenceArr.toArray(nCount);
        int[] len = new int[nCount];
        IntByReference intByReference = new IntByReference();
        int flagCount = DllService.INSTANCE.LSM_Get3DPointCloud(1,arr,len,intByReference);
        System.out.println("获取点云返回状态："+flagCount);
        //扫描头恢复原始位置
        System.out.println("test dll start LSM_Start3DScanOnce():"+DllService.INSTANCE.LSM_Start3DScanOnce(1));

        if(1==flagCount) {
            //取消将数据存在redis中
//            RedisOps.setObject("test",arr);
            return arr;

        }else {
        	System.out.println("未获取到点云数据");
        	return null;
        }
    }

    /**
     * 设置运行
     * @return
     */
    public String getLmsIsRunningOk(){
        boolean bo = DllService.INSTANCE.LSM_IsThreadRunningOK(1);
        return "LSM_IsThreadRunningOK:"+bo;
    }

    /**
     * 设置参数
     * @return
     */
    public String getLmsSetScanParameter(){
        int bo = DllService.INSTANCE.LSM_SetScanParameter(1,100,160,5);
        return "LSM_SetScanParameter:["+bo+"]";
    }

    /**
     * 设备开始执行
     * @return
     */
    public String getLmsStart3DScanOnce(){
        int bo = DllService.INSTANCE.LSM_Start3DScanOnce(1);
        return "LSM_Start3DScanOnce:["+bo+"]";
    }

    /**
     * 获取点云数据
     * @return
     */
    public Point3DTools getLms3DPointCloud(){
        int nCount = 500000;
        Point3D point3DReferenceArr=new Point3D();
        Point3D[] arr = (Point3D[])point3DReferenceArr.toArray(nCount);
        int[] len = new int[nCount];
        IntByReference intByReference = new IntByReference();
//        int flagCount = DllService.INSTANCE.LSM_Get3DPointCloud(1,arr,len,intByReference);
        int flagCount = DllService.INSTANCE.LSM_GetNewest3DPointCloud(1,arr,len,intByReference);
        Point3DTools point3DTools = new Point3DTools();
        point3DTools.setStatus(flagCount);
        point3DTools.setArr(arr);
        return point3DTools;
    }

    public static void main(String[] args) throws IOException {



    }

}

